<template>
  <div class="app-container">
    <div id="head">
      <div class="block">
        <el-form :inline="true" :model="classify" class="demo-form-inline">
          <el-form-item label="领导名称">
            <el-input v-model="leaderName" clearable placeholder="领导名称" />
          </el-form-item>
          <el-button type="primary" class="ml-1" @click="search">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="addad('ruleForm')">添加领导</el-button>
          <el-button v-show="delshow" type="danger" icon="el-icon-delete" @click="delselect">批量删除</el-button>
        </el-form>
      </div>
      <el-dialog top="10vh" title="添加领导" width="35%" :visible.sync="dialogFormVisible" :before-close="handleClose">
        <el-form ref="ruleForm" :model="form">
          <el-form-item label="领导图片" style="margin-bottom: 20px" :label-width="formLabelWidth" prop="logo">
            <el-upload
              ref="upload"
              class="avatar-uploader"
              action="https://app.xiangban365.com/app_mobile/upload/file"
              :show-file-list="false"
              :on-error="handleAvatarError"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="form.leaderImage" :src="form.leaderImage" class="avatar">
              <i v-else style="border: 1px dashed #d9d9d9" class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
          <el-form-item label="领导名称" style="margin-bottom: 10px" :label-width="formLabelWidth" prop="name">
            <el-input v-model="form.leaderName" class="input" autocomplete="off" />
          </el-form-item>
          <el-form-item label="领导职位" :label-width="formLabelWidth">
            <el-input v-model="form.position" class="input" autocomplete="off" />
          </el-form-item>
          <el-form-item label="排序" :label-width="formLabelWidth">
            <el-input-number v-model="form.sort" :min="1" :max="20" label="描述文字" @change="handleChange" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="handleCancel1">取 消</el-button>
          <el-button type="primary" @click="promit">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <el-table
      :data="list"
      border
      style="width: 98%;margin-top: 20px"
      @selection-change="handleSelectionChange1"
    >
      <el-table-column
        align="center"
        type="selection"
        width="100"
      />
      <el-table-column
        type="index"
        align="center"
        label="序号"
        width="200"
      />
      <el-table-column
        align="center"
        prop="leaderImage"
        label="领导图片"
        width="320"
      >
        <template slot-scope="scope">
          <img :src="scope.row.leaderImage" width="40" height="40">
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="leaderName"
        label="领导名称"
        width="300"
      />
      <el-table-column
        align="center"
        prop="position"
        label="领导职务"
        width="300"
      />
      <el-table-column
        align="center"
        prop="act"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            id="sty4"
            plain
            size="mini"
            class="ml-1"
            @click="changed(scope.row.id)"
          ><span>修改</span></el-button>
          <el-popconfirm
            title="确定删除该领导吗？"
            @onConfirm="del(scope.row.id,scope.row.shopId)"
          >
            <el-button
              id="sty3"
              slot="reference"
              type="danger"
              size="mini"
              class="ml-1"
            >删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--修改商品-->
    <el-dialog top="9vh" title="修改领导信息" width="35%" :visible.sync="dialogFormVisible1" :before-close="handleClose">
      <el-form :model="form">
        <el-form-item label="领导图片" style="margin-bottom: 20px" :label-width="formLabelWidth">
          <el-upload
            ref="upload"
            class="avatar-uploader"
            action="https://app.xiangban365.com/app_mobile/upload/file"
            :show-file-list="false"
            :on-success="handleAvatarSuccess1"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="shopdata.leaderImage" :src="shopdata.leaderImage" class="avatar">
            <img v-else :src="shopdata.leaderImage" class="avatar">
          </el-upload>
        </el-form-item>
        <el-form-item label="领导名称" style="margin-bottom: 10px" :label-width="formLabelWidth" prop="name">
          <el-input v-model="shopdata.leaderName" class="input" autocomplete="off" />
        </el-form-item>
        <el-form-item label="领导职位" :label-width="formLabelWidth">
          <el-input v-model="shopdata.position" class="input" autocomplete="off" />
        </el-form-item>
        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input-number v-model="shopdata.sort" :min="1" :max="20" label="描述文字" @change="handleChange" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="ml-5" @click="handleCancel2">取 消</el-button>
        <el-button type="primary" class="ml-auto" @click="save(shopdata.id)">修改</el-button>
      </div>

    </el-dialog>
    <!--分页-->
    <el-pagination
      background
      :current-page="repage.pageNum"
      :page-sizes="[10, 50, 100, 200]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="repage.total"
      class="page"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      @prev-click="handlePrevClick"
      @next-click="handleNextClick"
    />

  </div>
</template>

<script>
import { getLeaderList, addOrUpdLeader, getLeaderInfo, delLeader } from '@/api/teacher'

export default {
  name: 'Teacherlead',
  data() {
    return {
      leaderName: '',
      classify: {
        shopId: ''
      },
      repage: '',
      delshow: false,
      pageNum: 1,
      pageSize: 10,
      prolist: '',
      ids: '',
      imageUrl1: '',
      classifyids: '',
      dialogFormVisible: false,
      dialogFormVisible1: false,
      shopdata: '',
      imageUrl: '',
      form: {
        leaderImage: '',
        leaderName: '',
        position: '',
        sort: ''
      },
      formLabelWidth: '80px',
      page: 1,
      list: []
    }
  },
  created() {
    this.change()
  },
  methods: {
    handleChange(value) {
      console.log(value)
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {
        })
    },
    addad() { // 新增
      this.dialogFormVisible = true
      this.form.leaderImage = ''
      this.form.leaderName = ''
      this.form.position = ''
      this.form.sort = 1
    },
    handleCancel2() {
      this.dialogFormVisible1 = false
    },
    handleCancel1() {
      this.$refs.ruleForm.clearValidate()
      this.dialogFormVisible = false
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.change()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.change()
      console.log(`当前页: ${val}`)
    },
    handlePrevClick() {
      this.pageNum--
      this.change()
    },
    handleNextClick() {
      this.pageNum++
      this.change()
    },
    handleSelectionChange1(val) {
      console.log(val.length)
      this.ids = []
      if (val.length > 0) {
        val.forEach(res => {
          this.ids.push(res.id)
          console.log(this.ids)
        })
      }
      this.delshow = val.length > 0
    },
    // 批量删除
    delselect() {
      const data = {
        id: this.ids
      }
      delLeader(data).then(res => {
        console.log(res)
        if (res.success) {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.change()
        }
      })
    },
    // 修改
    changed(id) {
      this.show3 = false
      this.dialogFormVisible1 = true
      this.shopdata = []
      const data = {
        id: id
      }
      getLeaderInfo(data).then(res => {
        console.log(res)
        if (res.success) {
          this.shopdata = res.dataMap
        }
      })
    },
    // 修改保存
    save(id) {
      if (this.shopdata.leaderImage === '' || this.shopdata.leaderName === '' || this.shopdata.position === '') {
        this.$message('请填写完整信息')
        return
      } else {
        addOrUpdLeader(this.shopdata).then(res => {
          if (res.success) {
            console.log(res)
            this.dialogFormVisible1 = false
            this.change()
            this.$message({
              message: '修改成功',
              type: 'success'
            })
          } else {
            this.$message.error('操作失败')
          }
        })
      }
    },
    // 删除
    del(id, shopId) {
      const data = {
        id: [],
        shopId: shopId
      }
      data.id = []
      data.id.push(id)
      delLeader(data).then(res => {
        if (res.success) {
          this.change()
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        } else {
          this.$message.error('操作失败')
        }
      })
    },
    // 商品列表的展示
    change() {
      const data = {
        leaderName: this.leaderName
      }
      getLeaderList(data, this.pageNum, this.pageSize).then(res => {
        console.log(res)
        this.repage = res.dataMap
        this.list = res.dataMap.list
      })
    },
    // 查询
    search() {
      this.pageNum = 1
      this.change()
    },
    handleAvatarSuccess1(res, file) {
      console.log(res)
      this.shopdata.leaderImage = res.dataMap
    },
    handleAvatarSuccess(res, file) {
      console.log(res)
      this.form.leaderImage = res.dataMap
    },
    handleAvatarError(err, file, fileList) {
      console.log(err)
    },
    beforeAvatarUpload(file) {
      var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
      const extension =
        testmsg === 'jpg' ||
        testmsg === 'JPG' ||
        testmsg === 'png' ||
        testmsg === 'PNG' ||
        testmsg === 'bpm' ||
        testmsg === 'BPM'
      const isLt50M = file.size / 1024 / 1024 < 10
      if (!extension) {
        this.$message({
          message: '上传图片只能是jpg / png / bpm格式!',
          type: 'error'
        })
        return false // 必须加上return false; 才能阻止
      }
      console.log(file)
      if (!isLt50M) {
        this.$message({
          message: '上传文件大小不能超过 10MB!',
          type: 'error'
        })
        return false
      }
      return extension || isLt50M
    },
    // 新增提交
    promit() {
      if (this.form.leaderImage === '' || this.form.leaderName === '' || this.form.position === '') {
        this.$message('请填写完整信息')
        return
      } else {
        addOrUpdLeader(this.form, this.pageSize, this.pageNum).then(res => {
          if (res.success) {
            this.dialogFormVisible = false
            this.change()
            this.$message({
              message: '添加成功',
              type: 'success'
            })
          } else {
            this.$message.error('操作失败')
          }
        })
      }
    }
  }
}
</script>

<style scoped>
.page{
  margin-top: 20px;
  margin-right: 20px;
  float: right;
}
.input{
  width: 300px;
}
.block{
  margin-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

#sty3{
  border:1px solid #ff7f5b;
}

element.style {
  margin-top: 10vh;
}

.el-table td div {
  margin-left: 10px;
}

form {
  /*width: 80%;*/
  margin: auto;
}

#sty4 {
  border:1px solid #ff7f5b;
  color: #ff7f5b;
}

#sty2 {
  /*display: none;*/
}

.el-input[data-v-da9eedb6] {
  width: 17%;
  float: left;
  overflow: hidden;
}

#sty1, #sty2, #sty3, #sty4 {
  margin-right: 0;
}

#head {
  width: 100%;
  height: 30px;
}

.el-table .cell {
  padding-left: 42px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>

